package org.example.study.liaoxuefeng;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: 默苍璃
 * @date: 2023-05-23 20:33
 */
public class MyCondition {

    public static void main(String[] args) throws InterruptedException {

        TaskQueueCondition q = new TaskQueueCondition();

        ArrayList<Thread> ts = new ArrayList<Thread>();

        //循环5次  创建5个t线程 处于wait状态
        for (int i = 0; i < 5; i++) {
            Thread t = new Thread() {

                public void run() {
                    System.out.println(Thread.currentThread().getName());

                    // 执行task:
                    while (true) {
                        try {
                            String s = q.getTask();
                            System.out.println("execute task: " + s);
                        } catch (InterruptedException e) {
                            return;
                        }
                    }
                }
            };

            if (i == 0) {
                t.setPriority(Thread.MAX_PRIORITY);
            }

            if (i == 4) {
                t.setPriority(Thread.MIN_PRIORITY);
            }
            t.start();
            ts.add(t);
        }


        //创建add线程
        Thread add = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                // 放入task:
                String s = "t-" + Math.random();
                System.out.println("add task: " + s);
                System.out.println(Thread.currentThread().getName());
                System.out.println(Thread.currentThread().getPriority());
                q.addTask(s);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
            }
        });

        //启动add线程
        add.start();
        add.join();

        Thread.sleep(100);

        //终止(阻断)线程
        for (Thread t : ts) {
            t.interrupt();
        }

        System.out.println("__________________________________________________________________________________");
        TaskQueueCondition q1 = new TaskQueueCondition();

        Thread t1 = new Thread(() -> {
            try {
                String task1 = q1.getTask1();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            try {
                q1.addTask1("66");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t2.start();
    }
}


class TaskQueueCondition {
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
    private Queue<String> queue = new LinkedList<>();

    public void addTask(String s) {
        lock.lock();
        try {
            queue.add(s);
            Thread.sleep(3000);
            condition.signalAll();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public String getTask() throws InterruptedException {
        System.out.println("getTask____________" + Thread.currentThread().getName());

        lock.lock();
        try {
            while (queue.isEmpty()) {
                if (condition.await(1, TimeUnit.SECONDS)) {
                    // 被其他线程唤醒
                    System.out.println("未超时");
                } else {
                    // 指定时间内没有被其他线程唤醒
                    System.out.println("超时");
                }
            }
            return queue.remove();
        } finally {
            lock.unlock();
        }
    }


    public void addTask1(String s) throws InterruptedException {
        lock.lock();
        try {
            Thread.sleep(2000);
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }

    /**
     * 唤醒超时
     *
     * @return
     * @throws InterruptedException
     */
    public String getTask1() throws InterruptedException {
        System.out.println("getTask____________" + Thread.currentThread().getName());

        if (condition.await(1, TimeUnit.SECONDS)) {
            // 被其他线程唤醒
            System.out.println("未超时11");
        } else {
            // 指定时间内没有被其他线程唤醒
            System.out.println("超时11");
        }
        return "";
    }


}